﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： DCVehicleViolationQueryRepository.cs
// 项目名称：斑马车务
// 创建时间：2019/8/16
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using Dapper.Extensions;

namespace YouWei.Infrastructure.Repository
{
	/// <summary>
	///车辆违章查询 仓储层
	/// </summary>	
	public class DCVehicleViolationQueryRepository: BaseRepository<DCVehicleViolationQuery>
	{		
    
        #region 构造

        public DCVehicleViolationQueryRepository() : base(){}

        public DCVehicleViolationQueryRepository(string connectionString) : base(connectionString){}

        public DCVehicleViolationQueryRepository(DatabaseContext dbContext) : base(dbContext){}

        #endregion

        /// <summary>
        /// 未查询数据
        /// </summary>
        /// <returns></returns>
        public DCVehicleViolationQuery GetFirstNoRun()
        {
            string sql = "SELECT TOP 1 * FROM [DCVehicleViolationQuery] WITH(NOLOCK) WHERE QueryStatus=0 ORDER BY QueryID ASC";
            return FirstOrDefault(sql);
        }

        /// <summary>
        /// 获取最近一段时间内最新一条查询记录
        /// </summary>
        /// <param name="hpzl"></param>
        /// <param name="hphm"></param>
        /// <param name="enginecode"></param>
        /// <param name="lastTime"></param>
        /// <returns></returns>
        public DCVehicleViolationQuery GetLastTimeQuery(string hpzl,string hphm, string enginecode, DateTime lastTime)
        {
            string sql = @"SELECT *
                            FROM [DCVehicleViolationQuery] WITH(NOLOCK)
                            WHERE VehicleType = @hpzl
                                    AND VehicleLicense = @hphm
                                    AND EngineCode = @enginecode
                                    AND CreatedTime> @CreatedTime
                            ORDER BY  QueryID DESC";
            return FirstOrDefault(sql, new { hpzl = hpzl, hphm = hphm, enginecode= enginecode, CreatedTime = lastTime });
        }

        /// <summary>
        /// 根据手机号查询所有查询记录  分页
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="hphm"></param>
        /// <returns></returns>
        public Page<DCVehicleViolationQuery> GetPageByMobile(string mobile, int pageIndex, int pageSize, string hphm)
        {
            var strWhere = " Mobile =  '" + mobile + "'";
            if (!string.IsNullOrWhiteSpace(hphm))
            {
                strWhere += "  AND CHARINDEX('" + hphm + "',o.VehicleLicense)>0";
            }
            string sql = @"SELECT  o.*
	                            FROM DCVehicleViolationQuery o
	                            WITH (NOLOCK)
	                            WHERE " + strWhere + " ORDER BY QueryID DESC";
            return Pages(pageIndex, pageSize, sql);
        }
    }
}